package jMiiEditor.gui.miifilechooser;

import java.awt.Component;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFileChooser;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileType;
import net.sf.vfsjfilechooser.VFSJFileChooser;
import net.sf.vfsjfilechooser.acessories.DefaultAccessoriesPanel;
import net.sf.vfsjfilechooser.filechooser.VFSFileFilter;

public class MiiFileChooserVFSImpl 
    implements MiiFileChooser
{
    private final VFSJFileChooser fileChooser = new VFSJFileChooser();
    
    public final static VFSFileFilter miiFileFilter = new VFSFileFilter()
    {
        @Override
        public boolean accept( FileObject fo )
        {
            //VFSJFileChooser.
            //System.out.println("fo=" + fo);

            try {
                if( fo.getType().equals( FileType.FOLDER ) ) {
                    return true;
                }
            }
            catch( FileSystemException ignore ) {
                // Can't define type
            }
//            try {
//                fo.getChildren(); // Is there childs ?
//                return true;
//            }
//            catch( FileSystemException e ) {
//            }

            String filename = fo.toString().toLowerCase();
            
            return  filename.endsWith( ".mii" )
                    ||
                    filename.endsWith( ".mae" );
        }
        @Override
        public String getDescription()
        {
            return "Mii files (*.mii,*.mae)";
        }
        
        public String toString()
        {
            return getDescription();
        }
    };

    public MiiFileChooserVFSImpl()
    {
        // configure the file dialog
        fileChooser.setAccessory( new DefaultAccessoriesPanel( fileChooser ) );
        fileChooser.setFileHidingEnabled( false );
        fileChooser.setMultiSelectionEnabled( false );
        fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY );
        fileChooser.setFileFilter( miiFileFilter );
    }

    /**
     * 
     * @param parent
     * @return a new Mii from selected file !
     * @throws NullPointerException
     * @throws FileNotFoundException
     * @throws IOException
     */
    public MiiFileObject showOpenDialogExt( Component parent ) throws NullPointerException,
            FileNotFoundException, IOException
    {
        // show the file dialog
        int answer = fileChooser.showOpenDialog( parent );

        // check if a file was selected
        if( answer == JFileChooser.APPROVE_OPTION ) {
            final FileObject aFileObject = fileChooser.getSelectedFile();

            return new MiiFileObjectVFSImpl( aFileObject );
        }

        return null;
    }

    public MiiFileObject showSaveDialogExt( Component parent )
            throws  NullPointerException, 
                    FileNotFoundException,
                    IOException
    {
        // show the file dialog
        int answer = fileChooser.showOpenDialog( parent );

        // check if a file was selected
        if( answer == JFileChooser.APPROVE_OPTION ) {
            final FileObject aFileObject = fileChooser.getSelectedFile();

            return new MiiFileObjectVFSImpl( aFileObject );
        }
        
        return null;
    }

}
